package cn.itcast.web;

import cn.itcast.mapper.withdrawMapper;
import cn.itcast.pojo.*;
import cn.itcast.service.impl.PayServiceImpl;
import cn.itcast.util.OutputStringController;
import cn.itcast.util.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value="/wxshop/user",produces="application/json;charset=UTF-8")
public class payController {
    @Autowired
    private StringRedisTemplate template;
    @Resource
    PayServiceImpl payService;
    @Resource
    OutputStringController osc;

    @Resource
    private withdrawMapper withdrawMapper;


    /**
     * 功能描述: <br>
     * 〈获取骑手提现记录〉
     * @Author: lyy
     * @Date: 2020/8/16 10:23
     * @Param: [openId]
     * @Return: cn.itcast.util.ResponseEntity
     */
    @RequestMapping(value = "/horsemanFindWithdraw")
    public ResponseEntity horsemanFindWithdraw(@RequestParam(value = "openId") String openId){

        log.info("FUCK报错进来了嘛~~~~");

        withdrawExample wEx = new withdrawExample();
        withdrawExample.Criteria criteria = wEx.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        criteria.andUserTypeEqualTo("0");
        criteria.andStateEqualTo("已申请");
        List<withdraw> list = withdrawMapper.selectByExample(wEx);
        if(list.size()!=0){
            return new ResponseEntity(400,"该骑手已申请提现过！！！");
        }else{
            return new ResponseEntity(200);
        }
    }


    /**
     *
     * @Description:
     * 获取骑手账户金额
     * @auther: 欧阳辉
     * @date: 11:33 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findHoresmanAccount")
    public String findHoresmanAccount(@RequestBody Map<String,String> map){
        log.debug("获取骑手账户金额！！！");
        String openId = map.get("openId");
        //返回结果
        String results ="";
        List<account> accounts = payService.findHoresmanAccount(openId);
        if (accounts.isEmpty()) {
            results = osc.resultFailure("no", "");
            log.debug("获取骑手账户金额失败");
        } else {
            results = osc.resultSuccess("yes", accounts.get(0));
            log.debug("获取骑手账户金额成功");
        }
        return results;

    }

    /**
     *
     * @Description:
     * 获取骑手充值记录
     * @auther: 欧阳辉
     * @date: 11:33 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findrechargeRecord")
    public String findrechargeRecord(@RequestBody Map<String,String> map){
        log.debug("获取骑手充值记录！！！");
        String openId = map.get("openId");
        //返回结果
        String results ="";
        List<recharge> recharges = payService.findrechargeRecord(openId);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        //格式化申请时间、到账时间、转账时间
        for (recharge recharge : recharges) {
            if(recharge.getBillTime() != null){
                recharge.setBillTime_string(sdf.format(recharge.getBillTime()));
            }
            if(recharge.getRechargeTime() != null){
                recharge.setRechargeTime_string(sdf.format(recharge.getRechargeTime()));
            }
        }
        if (recharges.isEmpty()) {
            results = osc.resultFailure("no", "");
            log.debug("获取骑手充值记录失败");
        } else {
            results = osc.resultSuccess("yes", recharges);
            log.debug("获取骑手充值记录成功");
        }
        return results;

    }

    /**
     *
     * @Description:
     * 获取用户提现记录
     * @auther: 欧阳辉
     * @date: 11:33 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findWithdrawRecord")
    public String findWithdrawRecord(@RequestBody Map<String,String> map){
        log.debug("获取用户提现记录！！！");
        String openId = map.get("openId");
        //返回结果
        String results ="";
        List<withdraw> withdraws = payService.findWithdrawRecord(openId);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        //格式化申请时间、到账时间、转账时间
        for (withdraw withdraw2 : withdraws) {
            if(withdraw2.getApplyTime() != null){
                withdraw2.setApplyTime_string(sdf.format(withdraw2.getApplyTime()));
            }
            if(withdraw2.getBillTime() != null){
                withdraw2.setBillTime_string(sdf.format(withdraw2.getBillTime()));
            }
            if(withdraw2.getTransfer() != null){
                withdraw2.setTransfer_string(sdf.format(withdraw2.getTransfer()));
            }
        }
        if (withdraws.isEmpty()) {
            results = osc.resultFailure("no", "");
            log.debug("获取用户提现记录失败");
        } else {
            results = osc.resultSuccess("yes", withdraws);
            log.debug("获取用户提现记录成功");
        }
        return results;

    }




    /**
     *
     * @Description:
     * 获取用户账户
     * @auther: 欧阳辉
     * @date: 11:33 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findUserAccount")
    public String findUserAccount(@RequestBody Map<String,String> map){
        log.debug("获取用户账户金额！！！");
        String openId = map.get("openId");
        //返回结果
        String results ="";
        account account = payService.queryAccountByOpenIdByUser(openId);
        if (account==null) {
            results = osc.resultFailure("no", "");
            log.debug("获取用户账户金额失败");
        } else {
            results = osc.resultSuccess("yes", account);
            log.debug("获取用户账户金额成功");
        }
        return results;


    }


}
